'use strict';

const Controller = require('egg').Controller;

const appid = "wxa22f45356d82caf9";
const secret = "9632a29e1617d46c6ee4de1b26ad1972";

class YxcgController extends Controller {
  async echo() {
    
  }
  async getByParent(){
    const {ctx} = this;
    const query = ctx.query;
    let parentId = Number(query.parentId);
    if(!parentId) {
      return ctx.body = ctx.helper.formateResponse(null,500,"parentId不合法");
    }
    const result = await ctx.model.Yxgk.findAll({
      where:{
        parentId:parentId,
        status:"0"
      }
    });

    const aim = await ctx.model.Yxgk.findByPk(parentId);
    let obj = aim.toJSON();

    let data = {
      id:obj.deptId,
      name:obj.deptName,
      children:[]
    }

    data.children = result.map(el=>{
      let obj = el.toJSON();
      return {
        id:obj.deptId,
        label:obj.deptName
      }
    });

    ctx.body = ctx.helper.formateResponse(data);
  }
  async getQuestion(){
    const {ctx} = this;
    let query = ctx.query;
    let deptId = Number(query.id);
    if(!deptId) {
      return ctx.body = ctx.helper.formateResponse(null,500,"id不合法")
    }

    const result = await ctx.model.GameTitle.findAll({
      where:{
        deptId:deptId
      }
    });
    ctx.body = ctx.helper.formateResponse(result);
  }
  async findOpenId(){
    const {ctx} = this;
    const {jsCode} = ctx.query;
    if(!jsCode) return ctx.body = ctx.helper.formateResponse(null,500,"jsCode不合法");
    let url = `https://api.weixin.qq.com/sns/jscode2session?appid=${appid}&secret=${secret}&js_code=${jsCode}&grant_type=authorization_code`;
    try {
      const result = await ctx.curl(url,{
        dataType: 'json',
      });
      if(result.status === 200){
        ctx.body = ctx.helper.formateResponse(result.data);
      }else{
        ctx.body = ctx.helper.formateResponse(result,500,"获取失败")
      }
    } catch (error) {
      ctx.body = ctx.helper.formateResponse(error,500,"连接失败")
    }
  }
  async addscore(){
    const {ctx} = this;
    let body = ctx.request.body;
    if(!body.openid) {
      return ctx.body = ctx.helper.formateResponse(null,500,"未获取用户信息");
    }
    let scoreData = await ctx.model.GameScore.findOne({
      where:{
        openId:body.openid,
        moduleId:body.moduleId
      }
    });
    let data = {
      openId:body.openid,
      score:body.score,
      nickName:body.nickName,
      avatarUrl:body.avatarUrl,
      moduleId:body.moduleId
    }
    if(!scoreData){
      await ctx.model.GameScore.create(data);
    }else{
      await scoreData.update(data)
    }
    ctx.body = ctx.helper.formateResponse(data);
  }
  async getscore(){
    const {ctx,app} = this;
    let moduleId = Number(ctx.query.moduleId);
    const Sequelize = app.Sequelize;
    let option = {
      where:{
        moduleId,
      },
      limit:5,
      order:[
        ["score","DESC"]
      ]
      // order:Sequelize.col("score")
    }
    let result = await ctx.model.GameScore.findAll(option);
    ctx.body = ctx.helper.formateResponse(result);
  }
}

module.exports = YxcgController;
